<?php
/**
 * 客户基本信息上传
 */
namespace app\adminweb\controller;
use \think\Controller;
use \think\Validate;
header("Access-Control-Allow-Origin: * ");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
class Index
{
    public function index(){
        return 123;
        $xml =  '<?xml version="1.0" encoding="GB2312"?><CMBC header="100" version="100" security="none" lang="chs" trnCode="qryDtl">  <requestHeader>     <dtClient>2014-01-16 08:25:48</dtClient>     <clientId>2200003220</clientId>     <userId>2200003220001</userId>     <userPswd>123123</userPswd>     <language>chs</language>     <appId>nsbdes</appId>     <appVer>100</appVer>  </requestHeader>  <xDataBody>     <trnid>389831948229</trnid>     <cltcookie></cltcookie>     <acntNo>600033029</acntNo>     <dateFrom>2014-01-13</dateFrom>     <dateTo>2014-01-15</dateTo>     <startNo>1</startNo>     <endNo>20</endNo>     <typeCode>0</typeCode>  </xDataBody></CMBC>';
        $url = 'http://192.168.1.127:8080/eweb/b2e/connect.do';
        $hearder = array('Content-type:text/xml');
        $ch = curl_init(); //初始化curl
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,$url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $hearder);
        curl_setopt($ch,CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);
        $response = curl_exec($ch);
        if (curl_errno($ch)){
            print curl_error($ch);
        }
        curl_close($ch);
        $info = $this -> xml2array($response);
        dump($info);exit;
        echo $response;

    }

    protected function xml2array($xml){
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
    }
    public function test(){
        return 1000;
    }

    public function excle(){
        return view();
    }



    function xml_encode($data, $charset = 'utf-8', $root = 'so') {
        $xml = '<?xml version="1.0" encoding="' . $charset .'"?>';
        $xml .= "<{$root}>";
        $xml .= array_to_xml($data);
        $xml .= "</{$root}>";
        return $xml;
    }

    function xml_decode($xml, $root = 'so') {
        $search = '/<(' . $root . ')>(.*)<\/\s*?\\1\s*?>/s';
        $array = array();
        if(preg_match($search, $xml, $matches)){
            $array = xml_to_array($matches[2]);
        }
        return $array;
    }

    function array_to_xml($array) {
        if(is_object($array)){
            $array = get_object_vars($array);
        }
        $xml = '';
        foreach($array as $key => $value){
            $_tag = $key;
            $_id = null;
            if(is_numeric($key)){
                $_tag = 'item';
                $_id = ' id="' . $key . '"';
            }
            $xml .= "<{$_tag}{$_id}>";
            $xml .= (is_array($value) || is_object($value)) ? array_to_xml($value) : htmlentities($value);
            $xml .= "</{$_tag}>";
        }
        return $xml;
    }

    function xml_to_array($xml) {
        $search = '/<(\w+)\s*?(?:[^\/>]*)\s*(?:\/>|>(.*?)<\/\s*?\\1\s*?>)/s';
        $array = array ();
        if(preg_match_all($search, $xml, $matches)){
            foreach ($matches[1] as $i => $key) {
                $value = $matches[2][$i];
                if(preg_match_all($search, $value, $_matches)){
                    $array[$key] = $this -> xml_to_array($value);
                }else{
                    if('ITEM' == strtoupper($key)){
                        $array[] = html_entity_decode($value);
                    }else{
                        $array[$key] = html_entity_decode($value);
                    }
                }
            }
        }
        return $array;
    }
}